package ms.server.api.service;

import ms.server.api.common.ApiContent;
import ms.server.api.bean.po.AedApiInfo;
import ms.server.api.mapper.AedApiInfoMapper;
import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author ybc
 * @Date 2021/6/24 17:03
 */
@Service
@Slf4j
public class AedApiInfoService {

    private final Cache<String, Object> caffeineCache;

    private final AedApiInfoMapper apiInfoMapper;

    @Autowired
    public AedApiInfoService(Cache<String, Object> caffeineCache,
                             AedApiInfoMapper apiInfoMapper) {
        this.caffeineCache = caffeineCache;
        this.apiInfoMapper = apiInfoMapper;
    }

    public List<AedApiInfo> selectAll(){
        return apiInfoMapper.selectAll();
    }

    public void putInfo2Cache(){
        List<AedApiInfo> aedApiInfos = this.selectAll();
        if (aedApiInfos.size() > 0){
            for (AedApiInfo aedApiInfo : aedApiInfos){
                log.info(">>>>> db api info : {}",aedApiInfo.toString() );
                caffeineCache.put(ApiContent.API_REQ_INFO_KEY + aedApiInfo.getApiName(),aedApiInfo.getApiKey());
                caffeineCache.put(ApiContent.API_REQ_INFO_SECRET + aedApiInfo.getApiName(),aedApiInfo.getApiSecret());

            }
        }
    }

    public Integer add(){
        AedApiInfo aedApiInfo = new AedApiInfo();
        aedApiInfo.setId("1000100001");
        aedApiInfo.setApiName("YBC");
        aedApiInfo.setApiKey("asdfqewrfasdfasd");
        aedApiInfo.setApiSecret("aweradfasdfasdfasf");
        aedApiInfo.setState(true);
        aedApiInfo.setCreateTime(new Date());
        return apiInfoMapper.insert(aedApiInfo);
    }

    public void testexp() {
        int i = 10 ;
        int j = 10/0;
    }
}
